﻿using System;
using System.Collections.Generic;
using System.Linq;
using XiaoweiV2;

namespace Xiaowei.Players.XiaoweiPlayerLib.QQFM
{
    public class QQFMList : IXiaoweiPlayerList2
    {
        public QQFMList()
        {

        }
        public struct GetMoreArgs
        {
            public Skill skill;
            public string PlayID;
        }
        public Skill Skill { get; set; }
        public PlayMode PlayMode { get; private set; } = PlayMode.OrderPlay;
        public bool TryToChangePlayMode(PlayMode playMode)
        {
            if (playMode == PlayMode.LoopPlay || playMode == PlayMode.RandomPlay)
            {
                return false;
            }
            PlayMode = playMode;
            return true;
        }

        public PlayMode SwitchPlayMode()
        {
            switch (PlayMode)
            {
                case PlayMode.SinglePlay:
                    PlayMode = PlayMode.OrderPlay;
                    break;
                case PlayMode.OrderPlay:
                    PlayMode = PlayMode.SinglePlay;
                    break;
                default:
                    break;
            }
            return PlayMode;
        }

        private XiaoweiPlayerItemBase currentItem;

        public XiaoweiPlayerItemBase CurrentItem
        {
            get
            {
                return currentItem;
            }
            set
            {
                if (currentItem != value)
                {
                    currentItem = value;
                    ResetSkipBehavior();
                    CurrentItemChanged?.Invoke(this, CurrentItem);
                }
            }
        }

        public List<XiaoweiPlayerItemBase> Collection { get; } = new List<XiaoweiPlayerItemBase>();

        public event EventHandler CanGoNextChanged;
        public event EventHandler CanGoPreChanged;
        public event EventHandler<XiaoweiPlayerItemBase> CurrentItemChanged;
        public event EventHandler<object> RequestGetMoreUp;
        public event EventHandler<object> RequestGetMoreDown;
        public event Action<string, string> RequestRefreshURL;
        public event Action<XiaoweiPlayerItemBase> RefreshURLEnd;

        public int Index
        {
            get
            {
                if(Collection.Contains(CurrentItem))
                {
                    return Collection.IndexOf(CurrentItem);
                }    
                return -1;
            }
        }

        private void ResetSkipBehavior()
        {
            if (Index >= Collection.Count - 1 && CanGetMoreDown == true)
            {
                CanGoNext = false;
            }
            else
            {
                CanGoNext = true;
            }
            if (Index <= 0 && CanGetMoreUp == true)
            {
                CanGoPre = false;
            }
            else
            {
                CanGoPre = true;
            }
        }

        public void GoNext()
        {
            if (Collection.Count > 0)
            {
                if(CurrentItem==null)
                {
                    CurrentItem = Collection[0];
                }
                else
                {
                    CurrentItem= Collection[(Index + 1) % Collection.Count];
                }
            }
        }

        public void GoPre()
        {
            if (Collection.Count > 0)
            {
                if (CurrentItem == null)
                {
                    CurrentItem = Collection[0];
                }
                else
                {
                    CurrentItem = Collection[(Index + Collection.Count - 1) % Collection.Count];
                }
            }
        }

        public void CurrentPlayEnd()
        {
            switch (PlayMode)
            {
                case PlayMode.SinglePlay:
                    CurrentItemChanged?.Invoke(this, CurrentItem);
                    break;
                case PlayMode.OrderPlay:
                    if(Index<Collection.Count-1)
                    {
                        GoNext();
                    }
                    break;
                default:
                    break;
            }
        }

        public void EnqueueFront(IXiaoweiPlayerList2 xiaoweiPlayerListBase)
        {
            if(xiaoweiPlayerListBase is QQFMList qqFMList)
            {
                for (int i = qqFMList.Collection.Count-1; i >= 0; i--)
                {
                    if(!Collection.Contains(qqFMList.Collection[i]))
                    {
                        Collection.Insert(0, qqFMList.Collection[i]);
                    }
                }
                HasMoreUp = qqFMList.HasMoreUp;
                ResetSkipBehavior();
            }
        }

        public void EnqueueBack(IXiaoweiPlayerList2 xiaoweiPlayerListBase)
        {
            if (xiaoweiPlayerListBase is QQFMList qqFMList)
            {
                foreach (var item in qqFMList.Collection)
                {
                    if (!Collection.Contains(item))
                    {
                        Collection.Add(item);
                    }
                    
                }
                HasMareDown = qqFMList.HasMareDown;
                ResetSkipBehavior();
            }
        }

        public void GetMoreUp()
        {
            if(CanGetMoreUp)
            {
                RequestGetMoreUp?.Invoke(this, new GetMoreArgs()
                {
                    PlayID = ((QQFMItem)Collection.First()).PlayId,
                    skill = Skill
                }
                );
            }
            
        }

        public void GetMoreDown()
        {
            if (CanGetMoreDown)
            {
                int index = Collection.Count-1;
                if(index<0)
                {
                    return;
                }
                RequestGetMoreDown?.Invoke(this, new GetMoreArgs()
                {               
                    PlayID = ((QQFMItem)Collection.Last()).PlayId,
                    skill= Skill
                }
                ) ;
                
            }
        }

        public void Suspend()
        {
        }

        public void Resumed()
        {
        }

        public void RefreshURL(IXiaoweiPlayerList2 xiaoweiPlayerListBase)
        {
            if(xiaoweiPlayerListBase is QQFMList list)
            {
                foreach (var item in list.Collection)
                {
                    if (Collection.Contains(item))
                    {
                        Collection[Collection.IndexOf(item)].MediaUri = item.MediaUri;
                    }
                    if (CurrentItem.Equals(item))
                    {
                        RefreshURLEnd?.Invoke(CurrentItem);
                    }
                }
            }
        }

        public void GetURL(string playID)
        {
            RequestRefreshURL?.Invoke(Skill.Id,playID);
        }

        public bool HasMoreUp { get; set; }
        public bool HasMareDown { get; set; }

        private bool canGoNext = true;
        public bool CanGoNext
        {
            get
            {
                return canGoNext;
            }
            set
            {
                if(canGoNext!=value)
                {
                    canGoNext = value;
                    CanGoNextChanged?.Invoke(this,EventArgs.Empty);
                }
            }
        }

        private bool canGoPre = true;
        public bool CanGoPre
        {
            get
            {
                return canGoPre;
            }
            set
            {
                if (canGoPre != value)
                {
                    canGoPre = value;
                    CanGoPreChanged?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public string Title => Skill.Name;

        public bool CanGetMoreUp => HasMoreUp;

        public bool CanGetMoreDown => HasMareDown;


    }
}
